<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="80px" @submit.native.prevent>
      <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane label="基本信息" name="first">
          <el-form-item label="标题" prop="title">
            <el-input v-model="pram.title" maxlength="150" placeholder="标题" />
          </el-form-item>
          <el-form-item label="描述" prop="description">
            <el-input v-model="pram.description" maxlength="255" placeholder="描述" />
          </el-form-item>
          <el-form-item label="分类" prop="category_id">
            <el-select v-model="pram.category_id" placeholder="分类" clearable style="width: 100%">
              <el-option
                v-for="(item,index) in categoryList"
                :key="index"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-row :gutter="24">
<!--            <el-col :span="12">-->
<!--              <el-form-item label="来源" prop="origin">-->
<!--                <el-input v-model="pram.origin" maxlength="150" placeholder="来源" />-->
<!--              </el-form-item>-->
<!--            </el-col>-->
            <el-col :span="12">
              <el-form-item label="排序">
                <el-input-number v-model="pram.sort" :min="0"/> <span style="color: #ccc">（数字越大越靠前）</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="封面图" prop="face_url">
            <div class="upLoadPicBox" @click="modalPicTap()" :disabled="isDisabled">
              <div v-if="pram.face_url" class="pictrue"><img :src="pram.face_url"></div>
              <div v-else class="upLoad pictrue">
                <i class="el-icon-camera cameraIconfont" />
              </div>
            </div>
          </el-form-item>
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="是否热门">
                <el-switch v-model="pram.is_hot" :active-value="1" :inactive-value="2" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="状态">
                <el-switch v-model="pram.status" :active-value="1" :inactive-value="0" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="内容" name="second">
          <el-form-item prop="content" class="rich-content">
            <Tinymce v-model="pram.content"></Tinymce>
          </el-form-item>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div class="btn-box">
      <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:notice:update','admin:notice:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
      <el-button @click="close">取消</el-button>
    </div>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce/index'
import * as noticeApi from '@/api/noticeApi.js'
import * as systemSetting from '@/api/systemSetting.js'
import {getToken} from "@/utils/auth";
import {Debounce} from '@/utils/validate'
import Templates from "../appSetting/wxAccount/wxTemplate/index";
export default {
  // name: "edit"
  components: { Templates, Tinymce},
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    allCategoryData: {
      type: Array
    },
  },
  data() {
    return {
      isDisabled: this.$route.params.isDisabled==='1'?true:false,
      constants:this.$constants,
      activeName:"first",
      otherInfoVisible: false,
      pram: {
        title: null,
        description:null,
        category_id:null,
        content:null,
        face_url:null,
        origin:null,
        is_hot:2,
        sort: 0,
        status:1,
        id: 0,
      },
      rules: {
        title: [{ required: true, message: '请填标题', trigger: ['blur', 'change'] }],
        category_id: [{ required: true, message: '请选择分类', trigger: ['blur', 'change'] }],
        //description: [{ required: true, message: '请填描述', trigger: ['blur', 'change'] }],
        //face_url: [{ required: true, message: '请上传封面图', trigger: ['blur', 'change'] }],
        content: [{ required: true, message: '请填内容', trigger: ['blur'] }],
      },
      myHeaders: { "X-Token": getToken() },

      categoryList:[],
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    // 点击图标
    modalPicTap (i,status) {
      const _this = this;
      if(_this.isDisabled) return;
      this.$modalUpload(function(img) {
        _this.pram.face_url = img[0].att_dir
      },'1', 'content')
    },

    handleClick(tab) {
      if (tab.name === 'second') {
        this.otherInfoVisible = true;
      }else{
        this.otherInfoVisible = false;
      }
    },

    initEditData() {
      this.categoryList = this.allCategoryData

      if (this.isCreate !== 1) return //编辑
      const { title, description, category_id, content, face_url, origin, is_hot, sort, status, id } = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.title = title
      this.pram.description = description
      this.pram.category_id = category_id
      this.pram.content = content
      this.pram.face_url = face_url
      this.pram.origin = origin
      this.pram.is_hot = is_hot
      this.pram.status = status
      this.pram.sort = sort
      this.pram.id = id
    },

    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid){
          setTimeout(() => {
            //使用定时器，防止出发此处判断__error时验证器的__error的class还未添加
            //console.log(document.getElementsByClassName('el-form-item__error').length)
            if (document.getElementsByClassName('el-form-item__error').length > 0) {
              let errorNode = document.getElementsByClassName('is-error')[0];//$('.is-error')
              //console.log(errorNode.parentNode.id)
              //如果html结构变了此处需要检查是否多级parentNode
              if (errorNode.parentNode.id === "pane-second") {
                this.activeName = "second"
                this.otherInfoVisible = true
              } else if (errorNode.parentNode.id === "pane-first") {
                this.activeName = "first"
                this.otherInfoVisible = false
              }
            }
          }, 200);
          return
        }
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      noticeApi.doAdd(this.pram).then(data => {
        this.$message.success('创建成功')
        this.$emit('hideEditDialog')
      })
    },
    handlerEdit() {
      noticeApi.doAdd(this.pram).then(data => {
        this.$message.success('更新成功')
        this.$emit('hideEditDialog')
      })
    },
  }
}
</script>

<style scoped lang="scss">
.pictrue{
  width: 150px;
  height: 150px;
  border: 1px dotted rgba(0,0,0,0.1);
  margin-right: 10px;
  position: relative;
  cursor: pointer;
  img{
    width: 100%;
    height: 100%;
  }
  video{
    width: 100%;
    height: 100%;
  }
}
.rich-content{
  width:99%;
  ::v-deep.el-form-item__content{
    margin-left:10px!important;
  }
}
</style>
